﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;
using System.ComponentModel;
using Hotel.WPF.HotelWCFSurIIS;
//using log4net;
//using Hotel.WPF.ui.ChaineHotel;

namespace Hotel.WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        /// <summary>
        /// Log4net
        /// </summary>
        //private log4net.ILog Logger = LogManager.GetLogger("Malaw.Logging");
        HotelClient srvService;

        public MainWindow()
        {
            InitializeComponent();

            //log4net.Config.XmlConfigurator.Configure();

            //Logger.Info("Démarrage de l'application Malaw");

        }

        private void initPage()
        {
            try
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (SendOrPostCallback)delegate { busyTool.IsBusy = true; },
                    null
                    );
                var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
                backgroundWorker.DoWork += (s, args) =>
                {
                    writeToBusyTool("Initialisation du webservice en cours...");
                    srvService = new HotelClient();
                    srvService.Open();
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        //dg_ChaineHotel.DataContext = srvService.getListChaineHotels();
                                    },
                                    null
                                    );


                };

                backgroundWorker.ProgressChanged += (s, args) => writeToBusyTool("");
                backgroundWorker.RunWorkerCompleted += (s, args) => Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                                    (SendOrPostCallback)
                                                                                    delegate
                                                                                    {
                                                                                        busyTool.IsBusy = false;
                                                                                        srvService.Close();
                                                                                    },
                                                                                    null);
                backgroundWorker.RunWorkerAsync();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "initPage", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void writeToBusyTool(string message)
        {
            Dispatcher.BeginInvoke(
                DispatcherPriority.Normal,
                (SendOrPostCallback)delegate
                {
                    busyTool.BusyContent = message;
                },
                null
                );
        }

        private void smnChaineHotel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var win = new Hotel.WPF.ui.ChaineHotel.W_ChaineHotel();
                win.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void smnQuitter_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void smnHotel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var win = new Hotel.WPF.ui.Hotelss.W_Hotel();
                win.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void mnClient_Click(object sender, RoutedEventArgs e)
        {
            var win = new Hotel.WPF.ui.Client.W_Client();

            win.ShowDialog();
        }

        private void smnCaracteristiqueH_Click(object sender, RoutedEventArgs e)
        {
            var win = new Hotel.WPF.ui.CaracteristikHotel.W_RefCaracteristiqueHotel();

            win.ShowDialog();
        }

        private void mnParametres_Click(object sender, RoutedEventArgs e)
        {
            //var win = new Hotel.WPF.ui.Parametrage.W_Parametrage();

            //win.ShowDialog();
            Page win = new Hotel.WPF.ui.Parametrage.P_Parametrage();
            win.Height = 570;
            win.Width = 900;
            frame1.Width = 980;
            frame1.Height = 600;
            frame1.Navigate(win);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                busyTool.Width = SystemParameters.WorkArea.Width;
                busyTool.Height = SystemParameters.WorkArea.Height;
                initPage();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
